package com.zy.mc_module.ui.login;

import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;

import com.zy.mc_module.R;
import com.zy.mc_module.base.App;
import com.zy.mc_module.base.BaseActivity;
import com.zy.mc_module.mvp.bean.BaseBean;
import com.zy.mc_module.mvp.bean.UserBean;
import com.zy.mc_module.mvp.contract.LoginContract;
import com.zy.mc_module.mvp.presenter.LoginPresenter;
import com.zy.mc_module.utils.AppTools;
import com.zy.mc_module.utils.NoDoubleUtil;
import com.zy.mc_module.utils.StatusBarUtil;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.view.CustomHeadLayout;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 18/12/12
 * 微信登录绑定手机号
 */
public class BindTelActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {

    @BindView(R.id.customhead)
    CustomHeadLayout customhead;
    @BindView(R.id.bindtel_title)
    TextView tv_title;
    @BindView(R.id.bindtel_et_tel)
    EditText et_tel;
    @BindView(R.id.bindtel_tv_sure)
    TextView tv_sure;

    private String tel, openid, unionid, wxname, wxhead;
    private boolean isNew;//手机号是否为新号

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bindtel;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void initView() {
        App.getInstance().addTmpActivity(this);
        StatusBarUtil.setImmersiveStatusBar(this);
        openid = AppTools.getHasKeyS(this, "openid");
        wxname = AppTools.getHasKeyS(this, "wxname");
        wxhead = AppTools.getHasKeyS(this, "wxhead");
        unionid = AppTools.getHasKeyS(this, "unionid");
        customhead.setLeftClickListener(view -> finish());
    }


    /***
     * 下一步 获取验证码
     */
    @OnClick(R.id.bindtel_tv_sure)
    public void sure() {
        if (NoDoubleUtil.isFastClick()) {
            //下一步-获取验证码
            tel = et_tel.getText().toString().trim();
            //查询用户手机号是否注册
            presenter.checkPhoneRegister(context, tel);
        }
    }

    /***
     * 获取验证码成功
     */
    @Override
    public void onGetCodeSuccess(String success) {
        Intent intent = new Intent(context, VerCodeActivity.class);
        intent.putExtra("tel", tel);
        intent.putExtra("openid", openid);
        intent.putExtra("unionid", unionid);
        intent.putExtra("wxname", wxname);
        intent.putExtra("wxhead", wxhead);
        intent.putExtra("title", "绑定手机号");
        intent.putExtra("isNew", isNew);
        startActivity(intent);
    }

    @Override
    public void onLoginSuccess(UserBean userBean) {
    }

    @Override
    public void onSuccess(boolean isSuc) {

    }

    @Override
    public <T> void onSuccess(T t, int type) {
        if (type == 1) {
            BaseBean<Boolean> baseBean = (BaseBean<Boolean>) t;
            if (null != baseBean.data) {
                //下一步-获取验证码
                tel = et_tel.getText().toString().trim();
                isNew = baseBean.data;
                presenter.getCode(context, "2", tel);
            }
        }
    }

    @Override
    public void onError(ThdException error) {
        ToastUtil.showShort(error.message);
    }
}
